package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaCaseRelatedPerson;
import com.lilosoft.business.precity.dao.UpProAcceptUnitMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProAcceptUnit;
import com.lilosoft.business.precity.models.UpProAcceptUnitExample;
import com.lilosoft.business.util.IDCard;
import com.lilosoft.core.utils.commons.StringUtils;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 申请法人信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 9:55
 */
@Service
public class UpProAcceptUnitService {
    @Autowired
    private UpProAcceptUnitMapper upProAcceptUnitMapper;

    public UpProAcceptUnit UpProAcceptUnitSynchronous(UpProAccept upProAccept, CgaCaseRelatedPerson cgaPerson){
        UpProAcceptUnit model= new UpProAcceptUnit();
        BeanUtils.copyProperties(upProAccept,model);

        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());
        model.setApplyertype(upProAccept.getApplyertype());
        model.setApplyername(cgaPerson.getEname()==null? upProAccept.getApplyername()==null? "申请姓名":upProAccept.getApplyername():cgaPerson.getEname());
        model.setLegal(cgaPerson.getKeyman()==null?"法人代表姓名":cgaPerson.getKeyman());
        model.setApplyerpagetype(upProAccept.getApplyerpagetype());
        model.setApplyerpagecode(upProAccept.getApplyerpagecode());

        String number=cgaPerson.getKeymanIdcard()==null? cgaPerson.getJbrCardNumber():cgaPerson.getKeymanIdcard();
        if (StringUtils.isNotEmpty(number)&& StringUtils.isEmpty(IDCard.IdentityCardVerification(number))) {
            model.setApplyerpagetype("111");
            model.setApplyerpagecode(number);
        }
        //model.setContactname(cgaPerson.getRelatedPersonName());
        //model.setContactmobile(cgaPerson.getMobile());

        List<UpProAcceptUnit> upProAcceptUnits = selectByProjectNo(model.getProjectno());
        if(null==upProAcceptUnits || upProAcceptUnits.size()<=0){
            upProAcceptUnitMapper.insertSelective(model);
        }
        return model;
    }

    public List<UpProAcceptUnit> selectByProjectNo(String projectNo){
        UpProAcceptUnitExample example= new UpProAcceptUnitExample();
        UpProAcceptUnitExample.Criteria criteria = example.createCriteria();
        criteria.andProjectnoEqualTo(projectNo);
        criteria.andDelStateEqualTo(0);
        return upProAcceptUnitMapper.selectByExample(example);
    }

    /**
     * 根据cdBatch 统计数据个数（申请法人信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch){
        UpProAcceptUnitExample example= new UpProAcceptUnitExample();
        UpProAcceptUnitExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProAcceptUnitMapper.countByExample(example);
    }

}
